package com.example.wyh.intelcheckbash;

import android.app.Activity;
import android.app.ActivityManager;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wyh.intelcheckbash.common.SysExitUtil;

import java.util.ArrayList;
import java.util.Set;

/**
 * Created by chen on 17/11/15.
 */

public class SelectDeviceActivity  extends AppCompatActivity {

    private BluetoothAdapter mBtAdapter;
    public Spinner mSelectDeviceSpinner;
    public ArrayList<String> mDeviceNames;
    public ArrayList<String> mDeviceAddress;
    public String mSelectDeviceAddress = "";
    public String preConnectDevice = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_select);
        initActionBar();
        SysExitUtil.activityList.add(SelectDeviceActivity.this);

        //更新版本号
        TextView select_device_version_name = (TextView)findViewById(R.id.select_device_version_name);
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(getPackageName(), 0);
            String appVersionName = info.versionName; // 版本名
            select_device_version_name.setText("颐达科技 "+appVersionName);
//            Log.e("currentVersionCode,appVersionName",currentVersionCode + " " + appVersionName);
        } catch (PackageManager.NameNotFoundException e) {
            // TODO Auto-generated catch blockd
            e.printStackTrace();
        }

        preConnectDevice = LocalStorage.getString(SelectDeviceActivity.this,"blueToothAddress");

        mBtAdapter = BluetoothAdapter.getDefaultAdapter();

        // Get a set of currently paired devices
        Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
        mDeviceAddress = new ArrayList<String>();
        mDeviceNames = new ArrayList<String>();

        // If there are paired devices, add each one to the ArrayAdapter
        if (pairedDevices.size() > 0) {
            for (BluetoothDevice device : pairedDevices) {
                mDeviceAddress.add(device.getAddress());
                mDeviceNames.add(device.getName());
            }
            mSelectDeviceAddress = mDeviceAddress.get(0);
        } else {
            Toast.makeText(SelectDeviceActivity.this,"没有配对的蓝牙设备，请先配对",Toast.LENGTH_SHORT);
        }
        mSelectDeviceSpinner = initSelectBlueSpinner();

        mSelectDeviceSpinner.setSelection(getPreSelectBlueToothIndex());

        Button confirm_select_btn = (Button)findViewById(R.id.confirm_select_device);
        confirm_select_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent data = new Intent();
                data.putExtra("select_device_address",mSelectDeviceAddress);
                SelectDeviceActivity.this.setResult(2004, data);
                SelectDeviceActivity.this.finish();
            }
        });
    }
    public int getPreSelectBlueToothIndex()
    {
        int index = 0;
        for (int i = 0; i < mDeviceAddress.size(); i ++)
        {
            if (preConnectDevice.equals(mDeviceAddress.get(i)))
            {
                index = i;
            }
        }
        return index;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent data = new Intent();
            data.putExtra("select_device_address","noSelectDevice");
            SelectDeviceActivity.this.setResult(2004, data);
        }
        return super.onKeyDown(keyCode, event);
    }

    public Spinner initSelectBlueSpinner() {
        final Spinner spinner = (Spinner) findViewById(R.id.select_paired_device);
        spinner.setPrompt("请选择蓝牙设备");
        spinner.setSelection(0);
        ArrayAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mDeviceNames);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                spinner.setSelection(position);
                mSelectDeviceAddress = mDeviceAddress.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        return spinner;
    }

    public  void  initActionBar(){
        android.support.v7.app.ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setTitle("");
            actionBar.setDisplayShowHomeEnabled(false);
            actionBar.setDisplayShowCustomEnabled(true);
            LayoutInflater inflator = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflator.inflate(R.layout.actionbar_custom,new LinearLayout(SelectDeviceActivity.this),false);
            android.support.v7.app.ActionBar.LayoutParams layout = new android.support.v7.app.ActionBar.LayoutParams(
                    android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT, android.support.v7.app.ActionBar.LayoutParams.MATCH_PARENT);
            actionBar.setCustomView(v, layout);
            Toolbar parent = (Toolbar) v.getParent();
            parent.setContentInsetsAbsolute(0, 0);
        }
        TextView actionBarTitle = (TextView) findViewById(R.id.action_bar_title);
        actionBarTitle.setText("连接设备");
        ImageView title_logo = (ImageView)findViewById(R.id.title_logo_id);
        title_logo.setImageResource(R.drawable.device_select_title);
        RelativeLayout bar_back_btn =  (RelativeLayout) findViewById(R.id.bar_back_btn);
        bar_back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent data = new Intent();
                data.putExtra("select_device_address","noSelectDevice");
                SelectDeviceActivity.this.setResult(2004, data);
                SelectDeviceActivity.this.finish();
            }
        });
    }



}
